<template>
	<view class="cart-container">
		<!-- 购物车商品列表 -->
		<view class="" v-for="(item, index) in cartList" :key="index">
			<uv-swipe-action>
				<uv-swipe-action-item :options="options">
					<view class="cart-item">
						<view class="item-left">
							<uv-checkbox-group>
								<uv-checkbox :checked="item.checked" :name="index"
									@change="() => toggleItemSelect(index)" activeColor="#32B9B3" :shape="'circle'"
									:customStyle="{ borderRadius: '8px', borderColor: '#32B9B3' }" />
							</uv-checkbox-group>
							<image :src="item.image" mode="aspectFill" class="item-image"></image>
						</view>
						<view class="item-center">
							<view class="item-title size-16 size-bold color-333">{{ item.title }}</view>
							<view class="item-price color-e9 size-14">
								<view class="price-normal">
									<span class="size-12 size-bold">￥</span>
									<span class="size-16 size-bold">{{ item.price.toFixed(2) }}</span>
								</view>
								<!-- <view class="price-strike">
									<span class="size-12">￥</span>
									<span class="size-12">{{item.primary}}</span>
								</view> -->
							</view>
						</view>
						<view class="item-right">
							<view class="trash-bor">
								<!-- <uv-icon name="trash" size="26" color="#32B9B3"></uv-icon> -->
							</view>
							<view class="">
								<uv-number-box v-model="item.quantity" @change="valChange" class="custom-number-box"
									button-size="28"></uv-number-box>
							</view>

						</view>
					</view>
				</uv-swipe-action-item>
			</uv-swipe-action>
		</view>
		<view class="goShopping">
			<view class="goShopping-btn" @click="handlegoShopping">去逛逛...</view>
		</view>

		<!-- 底部结算栏 -->
		<view class="cart-footer">
			<checkbox-group @change="toggleAllSelect">
				<label>
					<checkbox :checked="isAllSelected" color="#32B9B3" style="border-radius: 50%;" />
					<text class="color-333">全选</text>
				</label>
			</checkbox-group>
			<view class="total-price color-333">
				合计：¥{{ totalPrice.toFixed(2) }}
			</view>
			<view class="checkout-btn color-333" @click="checkout">
				去结算 ({{ selectedCount }})
			</view>
		</view>
		<!--悬浮客服按钮 -->
		<liu-drag-button style="z-index: 50;">
			<image src="../../static/icon/icon-kf.png" mode="" style="width: 100%;height: 100%;"></image>
		</liu-drag-button>

		<!-- 底部导航栏 -->
		<tabbarVue :currentIndex="3"></tabbarVue>
	</view>
</template>

<script>
	import tabbarVue from '../../components/tabbar/tabbar.vue';
	import notHave from '@/components/notHave/notHave.vue';
	export default {
		components: {
			tabbarVue,
			notHave
		},
		data() {
			return {
				cartList: [{
						title: '金枝玉叶吊坠',
						price: 18000,
						primary: 50000,
						quantity: 1,
						image: 'https://crazy3.top/images/avatar/狂三.png',
						checked: false,
					},
					{
						title: '仿和田玉手链仿和田玉手链仿和田玉手链仿和田玉手链',
						price: 1500,
						primary: 70000,
						quantity: 1,
						image: 'https://crazy3.top/images/avatar/狂三.png',
						checked: false,
					},
					{
						title: '古典红绳手链',
						price: 399,
						primary: 5000,
						quantity: 1,
						image: 'https://crazy3.top/images/avatar/狂三.png',
						checked: false,
					},
					{
						title: '金枝玉叶吊坠',
						price: 18000,
						primary: 5000,
						quantity: 1,
						image: 'https://crazy3.top/images/avatar/狂三.png',
						checked: false,
					},

				],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}]
			};
		},

		computed: {
			// 全选
			isAllSelected() {
				return this.cartList.every(item => item.checked);
			},
			selectedItems() {
				return this.cartList.filter(item => item.checked);
			},
			// 去结算数量
			selectedCount() {
				return this.selectedItems.length;
			},
			// 合计金额
			totalPrice() {
				return this.selectedItems.reduce((sum, item) => sum + item.price * item.quantity, 0);
			}
		},
		methods: {
			// 切换单个商品选中状态
			toggleItemSelect(index) {
				this.cartList[index].checked = !this.cartList[index].checked;
			},

			// 全选/取消全选
			toggleAllSelect() {
				const checked = !this.isAllSelected;
				this.cartList.forEach(item => item.checked = checked);
			},
			// 单品数量
			valChange(e) {
				// console.log('当前值为: ' + e.value)
			},

			// 结算
			checkout() {
				if (this.selectedCount === 0) {
					uni.showToast({
						title: '请至少选择一个商品',
						icon: 'none'
					});
					return;
				}
				uni.navigateTo({
					url: '/pages/orderConfirm/orderConfirm'
				});
			},
			handlegoShopping(){
				uni.switchTab({
					url:"/pages/class/class"
				})
			}
		}
	};
</script>

<style lang="scss">
	.cart-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		box-sizing: border-box;
		padding-bottom: 120rpx;
	}

	.uv-swipe-action-item__content {
		background-color: #f5f5f5 !important;
	}

	.cart-list {
		// background-color: #fff;
		// padding: 0 20rpx;
	}

	.cart-item {
		width: 85%;
		margin: 20rpx auto;
		padding: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		border-radius: 10rpx;

		.item-left {
			width: 40%;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.item-image {
				width: 150rpx;
				height: 150rpx;
				border-radius: 10rpx;
			}
		}


		.item-center {
			width: 30%;
			min-width: 40%;
			height: 150rpx;
			padding-left: 10rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;


			.item-title {
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}


			.item-price {
				display: flex;
				align-items: center;
				gap: 10rpx;



				.price-strike span {
					text-decoration: line-through;
					color: #999;
				}
			}

		}


		.item-right {
			width: 30%;
			height: 150rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: flex-end;

		}


	}

	.goShopping {
		width: 100%;
		display: flex;
		align-items: center;
		flex-direction: column;
		gap: 20rpx;

		.goShopping-btn {
			padding: 20rpx 70rpx;
			background-color: #32B9B3;
			color: white;
			border-radius: 40rpx;
		}
	}

	/* 底部结算栏 */
	.cart-footer {
		position: fixed;
		bottom: calc(60px + env(safe-area-inset-bottom));

		left: 0;
		width: 100%;
		background-color: #fff;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.1);
		z-index: 99;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 40rpx 30rpx;
		box-sizing: border-box;
		height: 100rpx;

		.total-price {}

		.checkout-btn {
			background-color: #32B9B3;
			color: white;
			padding: 0 30rpx;
			border-radius: 40rpx;
			line-height: 60rpx;
			height: 60rpx;
			font-size: 26rpx;
		}
	}
</style>